package model

import (
	"project/schedule/util"
)

func HasFreeJudgeLeader(general *General, project *Project) bool {
	scale := util.Scale(project.ProposedDay, general.ProjectScale)
	projectDepartment := project.Department
	startDate := project.ProposedStartDate
	endDate := util.WorkDayIndex2Value[util.WorkDayValue2Index[startDate]+project.ProposedDay-1]

	for _, employeeInfo := range general.Staff {
		employeeDepartment := employeeInfo.Department
		ifGroupJudge := employeeInfo.GroupJudge[scale]
		ifGroupLeader := employeeInfo.GroupLeader[scale]
		employeeWorkRegion := employeeInfo.WorkRegion
		employeeVacation := employeeInfo.Vacation
		// 优先检测本部门的组长、主审, 并且他既可以当主审又可以当组长(则可以节省人力), 并且项目日期与vacation、workRegion不冲突
		if projectDepartment == employeeDepartment && ifGroupJudge && ifGroupLeader &&
			!util.CrossOverMulti(startDate, endDate, employeeWorkRegion) &&
			!util.CrossOverMulti(startDate, endDate, employeeVacation) {
			return true
		}
	}

	for _, employeeInfo := range general.Staff {
		employeeDepartment := employeeInfo.Department
		ifLendOut := employeeInfo.LendOut
		ifGroupJudge := employeeInfo.GroupJudge[scale]
		ifGroupLeader := employeeInfo.GroupLeader[scale]
		employeeWorkRegion := employeeInfo.WorkRegion
		employeeVacation := employeeInfo.Vacation
		// 优先检测其他部门的组长、主审, 并且他既可以当主审又可以当组长(则可以节省人力), 并且项目日期与vacation、workRegion不冲突
		if projectDepartment != employeeDepartment && ifGroupJudge && ifGroupLeader && ifLendOut &&
			!util.CrossOverMulti(startDate, endDate, employeeWorkRegion) &&
			!util.CrossOverMulti(startDate, endDate, employeeVacation) {
			return true
		}
	}

	container := make(map[string]bool)
	container["groupLeader"] = false
	container["judge"] = false

	for _, employeeInfo := range general.Staff {
		employeeDepartment := employeeInfo.Department
		ifGroupJudge := employeeInfo.GroupJudge[scale]
		ifGroupLeader := employeeInfo.GroupLeader[scale]
		employeeWorkRegion := employeeInfo.WorkRegion
		employeeVacation := employeeInfo.Vacation
		// 项目日期与vacation、workRegion不冲突
		if util.CrossOverMulti(startDate, endDate, employeeWorkRegion) ||
			util.CrossOverMulti(startDate, endDate, employeeVacation) {
			continue
		}
		// 优先检测本部门的组长、主审, 他只能当主审或者组长
		if projectDepartment == employeeDepartment {
			if ifGroupLeader {
				container["groupLeader"] = true
			}
			if ifGroupJudge {
				container["judge"] = true
			}
		}
		if container["groupLeader"] && container["judge"] {
			return true
		}
	}

	for _, employeeInfo := range general.Staff {
		employeeDepartment := employeeInfo.Department
		ifGroupJudge := employeeInfo.GroupJudge[scale]
		ifGroupLeader := employeeInfo.GroupLeader[scale]
		ifLendOut := employeeInfo.LendOut
		employeeWorkRegion := employeeInfo.WorkRegion
		employeeVacation := employeeInfo.Vacation
		// 项目日期与vacation、workRegion不冲突
		if util.CrossOverMulti(startDate, endDate, employeeWorkRegion) ||
			util.CrossOverMulti(startDate, endDate, employeeVacation) {
			continue
		}
		// 优先检测其他部门的组长、主审, 他只能当主审或者组长, 并且他可以被外借
		if projectDepartment != employeeDepartment && ifLendOut {
			if ifGroupLeader {
				container["groupLeader"] = true
			}
			if ifGroupJudge {
				container["judge"] = true
			}
		}
		if container["groupLeader"] && container["judge"] {
			return true
		}
	}
	return false
}
